package cn.edu.hit.education.action;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import cn.edu.hit.education.pojo.Course;
import cn.edu.hit.education.pojo.ExSchedule;
import cn.edu.hit.education.pojo.Specialty;
import cn.edu.hit.education.pojo.User;
import cn.edu.hit.education.service.ICourseService;
import cn.edu.hit.education.service.IPropertyService;
import cn.edu.hit.education.service.ISpecialtyService;
import cn.edu.hit.education.service.IUserSpeciatyService;
import cn.edu.hit.education.service.SpecialtyServiceImpl;
import cn.edu.hit.education.service.UserSpeciatyServiceImpl;
import educationschedule.Activator;
import educationschedule.Application;

public class ExportScheduleAction extends Action {
	ICourseService courseService = (ICourseService)Application.applicationContext.getBean("courseServiceImpl");
	IPropertyService propertyService = (IPropertyService)Application.applicationContext.getBean("propertyServiceImpl");
	IUserSpeciatyService userSpeciatyService = Application.applicationContext.getBean(UserSpeciatyServiceImpl.class);
	ISpecialtyService specialtyService = Application.applicationContext.getBean(SpecialtyServiceImpl.class);
	private List<ExSchedule> exSchedules = new ArrayList<ExSchedule>();
	
	public ExportScheduleAction(List<ExSchedule> exSchedules){
		this.setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/toolbar/schedule_16px.png"));
		this.setToolTipText("导出进程表");
		this.setText("导出进程表");
		this.exSchedules = exSchedules;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		Specialty specialty = new Specialty();
		List<Specialty> specialties = new ArrayList<>();
		
		//根据普通用户的用户Id查询到关联的专业Id
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
		List<Integer> ids = userSpeciatyService.querySpeciatyIdsByUser(user.getId());
		specialties = specialtyService.queryRelativeSpecialty(ids);	
		
		specialty.setName(specialties.get(0).getName());
		
		Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		FileDialog fileDialog = new FileDialog(parentShell,SWT.SAVE);
		fileDialog.setFilterExtensions(new String[]{"*.xls"});
		String filename ;
		int row = 0;
		int col = 0;
		filename = fileDialog.open();
		if(filename != null){
			try {
				HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
		        HSSFSheet sheet = hssfWorkbook.createSheet();
		        HSSFRow hSSFRow = null;
		        HSSFCell hSSFCell = null;
		        Course course = null;
		        //////////////////////////////////////////////////
		        if(exSchedules != null && exSchedules.size() > 0){
		        	hSSFRow = sheet.createRow(row);
		        	//课程代码
	        		hSSFCell = hSSFRow.createCell(col);
	        		hSSFCell.setCellValue("课程代码");
	        		col++;
	        		//课程名称
	        		hSSFCell = hSSFRow.createCell(col);
	        		hSSFCell.setCellValue("课程名称");
	        		col++;
	        		//是否核心课
	        		hSSFCell = hSSFRow.createCell(col);
	        		hSSFCell.setCellValue("是否核心课");
	        		col++;
	        		//学分
	        		hSSFCell = hSSFRow.createCell(col);
	        		hSSFCell.setCellValue("学分");
	        		col++;
	        		//学时
	        		hSSFCell = hSSFRow.createCell(col);
	        		hSSFCell.setCellValue("学时");
	        		col++;
	        		//讲课
	        		hSSFCell = hSSFRow.createCell(col);
	        		hSSFCell.setCellValue("讲课");
	        		col++;
	        		//实践
	        		hSSFCell = hSSFRow.createCell(col);
	        		hSSFCell.setCellValue("实践");
	        		col++;
	        		//周学时
	        		hSSFCell = hSSFRow.createCell(col);
	        		hSSFCell.setCellValue("周学时");
	        		col++;
	        		//考核方式
	        		hSSFCell = hSSFRow.createCell(col);
	        		hSSFCell.setCellValue("考核方式");
	        		col++;
	        		//备注
	        		hSSFCell = hSSFRow.createCell(col);
	        		hSSFCell.setCellValue("备注");
	        		col++;
	        		
		        	row++;
		        	for (ExSchedule exSchedule : exSchedules) {
		        		if(exSchedule.getOpened() == 0){
		        			continue;
		        		}
		        		
		        		col = 0;
		        		course = exSchedule.getCourse();		        		
		        		hSSFRow = sheet.createRow(row);
		        		//课程代码
		        		hSSFCell = hSSFRow.createCell(col);
		        		hSSFCell.setCellValue(course.getNumber());
		        		col++;
		        		//课程名称
		        		hSSFCell = hSSFRow.createCell(col);
		        		hSSFCell.setCellValue(course.getName());
		        		col++;
		        		//是否核心课
		        		hSSFCell = hSSFRow.createCell(col);
		        		hSSFCell.setCellValue(course.getImportant() == 1 ? "√" : "");
		        		col++;
		        		//学分
		        		hSSFCell = hSSFRow.createCell(col);
		        		hSSFCell.setCellValue(exSchedule.getCredits());
		        		col++;
		        		//学时
		        		hSSFCell = hSSFRow.createCell(col);
		        		hSSFCell.setCellValue(exSchedule.getPeriod());
		        		col++;
		        		//讲课
		        		hSSFCell = hSSFRow.createCell(col);
		        		hSSFCell.setCellValue(exSchedule.getTheoretical());
		        		col++;
		        		//实践
		        		hSSFCell = hSSFRow.createCell(col);
		        		hSSFCell.setCellValue(exSchedule.getExperiment());
		        		col++;
		        		//周学时
		        		hSSFCell = hSSFRow.createCell(col);
		        		hSSFCell.setCellValue("");
		        		col++;
		        		//考核方式
		        		hSSFCell = hSSFRow.createCell(col);
		        		hSSFCell.setCellValue("");
		        		col++;
		        		//备注
		        		hSSFCell = hSSFRow.createCell(col);
		        		hSSFCell.setCellValue("");
		        		col++;
		        		//下一行
		        		row++;
					}
		        }
		        
		        ///////////////////////////////////////////////////
		        FileOutputStream out = new FileOutputStream(filename);
		        hssfWorkbook.write(out);
		        out.close();
			}catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		super.run();
	}
}
